/** Class for regression operations native to both local and global trending
 * 
 */
package trend;

import java.util.*;

/**
 * @author Hussein Patwa
 * @date 10 April 2008
 */
public class Regression {
    
    TreeMap predictions;
    
    public Regression(TreeMap pathObs){
        predictions = new TreeMap();
        double sigmaX = Utils.sigmaX(pathObs.keySet());
	double sigmaX2 = Utils.sigmaXSquare(pathObs.keySet());
	double sigmaY = Utils.sigmaY(pathObs.values());
	double sigmaXY = Utils.sigmaXY(pathObs);
	double slope = Utils.getLeastSquareSlope(sigmaX, sigmaX2, sigmaY, sigmaXY,
		pathObs.size());// compute the slope with sign
	double constantC = Utils.getLeastSquareC(sigmaX, sigmaX2, sigmaY, sigmaXY,
		pathObs.size());
	for (Iterator i = pathObs.entrySet().iterator(); i.hasNext();) {
	    Map.Entry e = (Map.Entry) i.next();
	    Double key = (Double) e.getKey();
	    double predictedVal = slope * key.doubleValue() + constantC;
	    predictions.put(key, new Double(predictedVal));
	}
    }
    
    TreeMap getRegressionPredictions() {
	return predictions;
    }
    
}
